// This file contains study-related API functions
import type { 
  UpdateProgressData, UpdateProgressResponses,
  CompleteCourseData, CompleteCourseResponses,
  StartStudyData, StartStudyResponses,
  GetUserStudySummaryData, GetUserStudySummaryResponses,
  GetStudyRecordData, GetStudyRecordResponses,
  GetUserStudyRecordsData, GetUserStudyRecordsResponses,
  GetStudyLeaderboardData, GetStudyLeaderboardResponses,
  GetInProgressCoursesData, GetInProgressCoursesResponses,
  GetCompletedCoursesData, GetCompletedCoursesResponses
} from './studyTypes';
import { client as _heyApiClient } from '../../api/client.gen';
import type { Options } from '../common/apiTypes';

/**
 * 更新学习进度
 * 更新用户的学习进度，包括已完成章节数和学习时长
 */
export const updateProgress = <ThrowOnError extends boolean = false>(options: Options<UpdateProgressData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).put<UpdateProgressResponses, unknown, ThrowOnError>({
        url: '/api/study/progress/{courseId}',
        ...options,
        headers: {
            'Content-Type': 'application/json',
            ...options.headers
        }
    });
};

/**
 * 完成课程学习
 * 标记课程为已完成状态，更新完成时间和进度
 */
export const completeCourse = <ThrowOnError extends boolean = false>(options: Options<CompleteCourseData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).put<CompleteCourseResponses, unknown, ThrowOnError>({
        url: '/api/study/complete/{courseId}',
        ...options
    });
};

/**
 * 开始学习课程
 * 用户开始新的课程学习，创建学习记录
 */
export const startStudy = <ThrowOnError extends boolean = false>(options: Options<StartStudyData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).post<StartStudyResponses, unknown, ThrowOnError>({
        url: '/api/study/start/{courseId}',
        ...options
    });
};

/**
 * 获取用户学习统计
 * 获取当前登录用户的学习统计数据
 */
export const getUserStudySummary = <ThrowOnError extends boolean = false>(options?: Options<GetUserStudySummaryData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetUserStudySummaryResponses, unknown, ThrowOnError>({
        url: '/api/study/summary',
        ...options
    });
};

/**
 * 获取学习记录
 * 获取用户指定课程的学习记录详情
 */
export const getStudyRecord = <ThrowOnError extends boolean = false>(options: Options<GetStudyRecordData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).get<GetStudyRecordResponses, unknown, ThrowOnError>({
        url: '/api/study/record/{courseId}',
        ...options
    });
};

/**
 * 获取用户的学习记录列表
 * 分页获取用户的所有学习记录
 */
export const getUserStudyRecords = <ThrowOnError extends boolean = false>(options: Options<GetUserStudyRecordsData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).get<GetUserStudyRecordsResponses, unknown, ThrowOnError>({
        url: '/api/study/my-records',
        ...options
    });
};

/**
 * 获取学习排行榜
 * 获取学习排行榜，支持日榜、周榜、总榜
 */
export const getStudyLeaderboard = <ThrowOnError extends boolean = false>(options?: Options<GetStudyLeaderboardData, ThrowOnError>) => {
    return (options?.client ?? _heyApiClient).get<GetStudyLeaderboardResponses, unknown, ThrowOnError>({
        url: '/api/study/leaderboard',
        ...options
    });
};

/**
 * 获取学习中的课程
 * 获取用户当前正在学习的课程列表
 */
export const getInProgressCourses = <ThrowOnError extends boolean = false>(options: Options<GetInProgressCoursesData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).get<GetInProgressCoursesResponses, unknown, ThrowOnError>({
        url: '/api/study/in-progress',
        ...options
    });
};

/**
 * 获取已完成的课程
 * 获取用户已完成的课程列表
 */
export const getCompletedCourses = <ThrowOnError extends boolean = false>(options: Options<GetCompletedCoursesData, ThrowOnError>) => {
    return (options.client ?? _heyApiClient).get<GetCompletedCoursesResponses, unknown, ThrowOnError>({
        url: '/api/study/completed',
        ...options
    });
};